<template>
	<view >
		<view class="padding-xl bg-white">
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">收货人 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.name" placeholder="请输入收货人" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">电话 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.phone" placeholder="请输入电话" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">省份 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.province" placeholder="请输入省份" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">城市 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.city" placeholder="请输入城市" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">地区 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.region" placeholder="请输入地区" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">详细地址 *:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.detailAddress" placeholder="请输入详细地址" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">邮编:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.postCode" placeholder="请输入邮编" />
			</view>
		</view>
		<view class="flex" style="height: 100upx;">
			<view style="width: 250upx;">区域编码:</view>
			<view style="width: 550upx;">
				<input v-model="newAddress.areacode" placeholder="请输入区域编码" />
			</view>
		</view>
		<view class="flex justify-between">
			<view style="width: 250upx;">设置为默认地址:</view>
			<checkbox :checked="newAddress.defaultStatus==1" :class="newAddress.defaultStatus==1?'checked':''" class="round sx-check"
			 style="margin-left: 40upx;" @click="change"></checkbox>
		</view>
		<view class="margin-top">
			<button v-if="isEdit" class="cu-btn bg-red" style="width: 650upx;" @click="editAddress">保存</button>
			<button v-else class="cu-btn bg-red " style="width: 650upx;" @click="addAddress">添加地址</button>
		</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				memberId: undefined,
				isEdit:false,
				newAddress: {
					memberId: undefined, // 用户ID
					name: '', // 姓名
					phone: '', // 电话
					postCode: '', // 邮编
					province: '', // 省份
					city: '', // 城市
					region: '', // 地区
					detailAddress: '', // 详细地址
					areacode: '', // 区域编码
					defaultStatus: 0 // 1是默认收获地址,0不是默认收获地址
				},
			}
		},
		onLoad(options) {
			if (options.hasOwnProperty('oldAddress')) {
				let oldAddress = JSON.parse(decodeURIComponent(options.oldAddress))
				for(let key in oldAddress) {
					if(key!='checked'){
						this.newAddress[key] = oldAddress[key]
					}
				}
				this.isEdit = true
			}
			this.memberId = Number(options.memberId)
			this.newAddress.memberId = options.memberId
			console.log('options', options)
			console.log('newAddress', this.newAddress)
		},
		methods: {
			change() {
				var i = this.newAddress.defaultStatus
				i==1?this.newAddress.defaultStatus = 0:this.newAddress.defaultStatus = 1
			},
			fun1() {
				let params = {}
				for (let item in this.newAddress) {
					if (this.newAddress[item] != '' && this.newAddress[item] != null) {
						var value = this.newAddress[item]
						if(item =='areacode'){
							value = Number(value)
						}
						params[item] = value
					}
				}
				params['defaultStatus'] = this.newAddress['defaultStatus']
				
				if (!params.hasOwnProperty('memberId') ||
					!params.hasOwnProperty('name') ||
					!params.hasOwnProperty('phone') ||
					!params.hasOwnProperty('province') ||
					!params.hasOwnProperty('city') ||
					!params.hasOwnProperty('region') ||
					!params.hasOwnProperty('detailAddress')){
						uni.showToast({
							title: '请输入必选项！',
							icon: 'none'
						})
						return false
				}
				console.log('params',params)
				return params
			},
			async addAddress() {
				let params = {}
				if(this.fun1() == false){
					return
				}else{
					params = this.fun1()
				}
				var res = await this.$request({
					url:`member/memberreceiveaddress/save`,
					params:params,
					type:"POST"
				})
				console.log(res)
				if(res.code == 0) {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					uni.redirectTo({
						url:`addressCRM?memberId=${this.memberId}`
					})
				}
			},
			async editAddress() {
				let params = {}
				if(this.fun1() == false){
					return
				}else{
					params = this.fun1()
				}
				var res = await this.$request({
					url: `member/memberreceiveaddress/update`,
					params: params,
					type: "POST"
				})
				console.log(res)
				if(res.code == 0) {
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					uni.redirectTo({
						url:`addressCRM?memberId=${this.memberId}`
					})
				}
			}
		}
	}
</script>

<style>
</style>
